10 map
<int, set
<string
> > dict
;
11 map
<char, char> solucion
;
14 bool contradice(const string
&origen
, const string
&destino
, map
<char, char> crypt
){
15 //cout << "Contradice llamado con " << origen << " y " << destino << endl;
18 for (int i
=0; i
<origen
.size(); ++i
){
19 if (temp
.count(origen
[i
]) == 1){
20 if (temp
[origen
[i
]] != destino
[i
]){
24 for (map
<char, char>::iterator j
= temp
.begin(); j
!= temp
.end(); ++j
){
25 if ((*j
).second
== destino
[i
]) return true;
27 temp
[origen
[i
]] = destino
[i
];
31 if (origen
.length() != destino
.length()) return true;
33 for (int i
=0; i
<origen
.size(); ++i
){
34 if (crypt
.count(origen
[i
]) == 1){
35 if (crypt
[origen
[i
]] != destino
[i
]){
39 for (map
<char, char>::iterator j
= crypt
.begin(); j
!= crypt
.end(); ++j
){
40 if ((*j
).second
== destino
[i
]) return true;
49 void asignar(const string
&origen
, const string
&destino
, map
<char, char> &crypt
){
50 if (origen
.size() != destino
.size()) return;
52 for (int i
=0; i
<origen
.size(); ++i
){
53 crypt
[origen
[i
]] = destino
[i
];
57 bool bt(int i
, const vector
<string
> &words
, map
<char, char> crypt
){
58 if (i
>= words
.size()){
63 int len
= words
[i
].length();
64 set
<string
> possible
= dict
[len
];
66 for (set
<string
>::iterator j
= possible
.begin(); j
!= possible
.end(); ++j
){
68 /* if (!contradice(words[i], (*j), crypt)){
69 map<char, char> copia = crypt;
70 asignar(words[i], (*j), copia);
72 if (bt(i+1, words, copia)){
79 if (!contradice(words
[i
], (*j
), crypt
)){
80 map
<char, char> copia
= crypt
;
81 asignar(words
[i
], (*j
), copia
);
83 if (bt(i
+1, words
, copia
)){
99 dict
[s
.length()].insert(s
);
100 //cout << "agregue " << s << " al dict en la posicion " << s.length() << endl;
104 while (getline(cin
, line
)){
105 //cout << "Lei esta linea: " << line << endl;
112 vector
<string
> words
;
117 words
.push_back(word
);
119 //cout << "Empuje la palabra " << word << " a la lista " << endl;
122 if (bt(0, words
, map
<char, char>() )){
123 for (int i
=0; i
<line
.size(); ++i
){
124 if (line
[i
] == ' ') cout
<< " ";
125 else cout
<< solucion
[line
[i
]];
128 //cout << "Hay solucion\n";
130 for (int i
=0; i
<line
.size(); ++i
){
131 if (line
[i
] == ' ') cout
<< " ";
135 //cout << "No hay solucion\n";